package com.nsgf.library.ui.page.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.bean.ProjectBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.data.bean.XBannerBeanInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.databinding.FragmentNsgfRegisterBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.glide.GlideImgManager;
import com.nsgf.library.photopicker.PhotoPreview;
import com.nsgf.library.photopicker.widget.MultiPickResultView;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.ToastUtils;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;


/**
 * 工程概况-注册登记
 */
public class ProjectOverviewRegisterFragment extends BaseFragment {

    public static ProjectOverviewRegisterFragment newInstance(ProjectBean projectBean) {
        ProjectOverviewRegisterFragment fragment = new ProjectOverviewRegisterFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("projectBean", projectBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    private ProjectBean getData() {
        return getArguments().getParcelable("projectBean");
    }

    private ProjectOverviewRegisterFragmentStates mStates;

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(ProjectOverviewRegisterFragmentStates.class);
        mAccountRequester = new AccountRequester();

    }


    @Override
    protected DataBindingConfig getDataBindingConfig() {

        return new DataBindingConfig(R.layout.fragment_nsgf_register, BR.vm, mStates);
    }

    private List<XBannerBeanInfo> banners = new ArrayList<>();

    @Override
    public void onViewCreated(@NonNull View v, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (FragmentNsgfRegisterBinding) getBinding();
        mStates.projectBeanState.set(getData());
        mAccountRequester.getRegisterInfo(UserInfo.getInstance().getProjectCode());

        mAccountRequester.getLibraryInfo().observe(this, new Observer<ResponseStatus<LibraryInfo>>() {
            @Override
            public void onChanged(ResponseStatus<LibraryInfo> libraryInfoResponseStatus) {

                if (libraryInfoResponseStatus.getResponseCode() == 200) {
                    if (!libraryInfoResponseStatus.getData().getList().isEmpty()) {

                        mStates.registerInfo.set(libraryInfoResponseStatus.getData().getList().get(0));

                    }
                }else {
                    ToastUtils.showLongToast(libraryInfoResponseStatus.getMsg());
                }

            }
        });

        banners.clear();
        if (null != mStates.projectBeanState.get().getFiles()) {
            for (WorkOrderDetailBean.FileDto fileDto : mStates.projectBeanState.get().getFiles()) {
                banners.add(new XBannerBeanInfo(APIs.BASE_URL_IMG + fileDto.getFilePath()));
            }
            mBinding.xbanner.setBannerData(banners);
            mBinding.xbanner.loadImage((banner, model, view, position) -> {
                String url = (String) ((XBannerBeanInfo) model).getXBannerUrl();
                GlideImgManager.getInstance().glideLoaderBannerCenterCrop(getActivity(),
                        url.replace("\\", "/"), (ImageView) view, 10);

            });
            mBinding.xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                @Override
                public void onItemClick(XBanner banner, Object model, View view, int position) {
                    ArrayList<WorkOrderDetailBean.FileDto> fileDtos = new ArrayList<>();
                    WorkOrderDetailBean.FileDto fileDto = new WorkOrderDetailBean.FileDto();
                    fileDto.setFilePath(banners.get(position).getXBannerUrl().toString());
                    fileDtos.add(fileDto);
                    PhotoPreview.builder()
                            .setPhotos(fileDtos)
                            .setAction(MultiPickResultView.ACTION_ONLY_SHOW)
                            .start(getActivity());
                }
            });
        }
    }

    private String search = "";


    private FragmentNsgfRegisterBinding mBinding;


    public static class ProjectOverviewRegisterFragmentStates extends StateHolder {

        public final State<String> name = new State<>("");

        public final State<List<WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal>> list = new State<>(new ArrayList<>());

        public final State<Boolean> loadingVisible = new State<>(false);


    }
}
